#include "../../Helix.h"
#include "../../HObject.h"
#include "Image.h"
#include "../../Log.h"
#include <string>
#include <vector>

using namespace std;

namespace helix
{


	Image::Image(const char *fileName) : HObject()
	{
		Log().Println("Attempting to load Image: %s",fileName);
		width = 0;
		height = 0;
		depth = 0;
		bpp = 0;
		channels = 0;
		loaded = false;
		imageType = "unknown";
		data = NULL;
	}

	Image::~Image()
	{
	}

	void Image::swapPixelComponents(int comp1, int comp2)
	{
		if(data==NULL || comp1<0 || comp1>=channels || comp2<0 || comp2>=channels)
		{
			loaded = false;
			Log().Error("Cannot swap components %d and %d of pixel data because they are outside bounds of the image format.",comp1,comp2);
			return;
		}
		if(comp1 == comp2)
		{
			Log().Println("Did not swap components for image because they are the same.");
			return;
		}

		unsigned int pixelCount = width * height * depth;
		for(unsigned int i=0;i<pixelCount;i++){
			int pixelStart=i*channels;
			unsigned char temp = (*data)[pixelStart + comp1];
			(*data)[pixelStart + comp1]=(*data)[pixelStart + comp2];
			(*data)[pixelStart + comp2]=temp;
		}

	}

	unsigned int Image::getWidth()
	{
		return width;
	}

	unsigned int Image::getHeight()
	{
		return height;
	}

	unsigned int Image::getDepth()
	{
		return depth;
	}
	
	int Image::getBitsPerPixel()
	{
		return bpp;
	}

	int Image::getChannels()
	{
		return channels;
	}

	string Image::getType()
	{
		return imageType;
	}

	unsigned char* Image::getData()
	{
		if(data!=NULL && data->size()>0)
			return &(*data)[0];

		else
			return NULL;
	}

	bool Image::isLoaded()
	{
		return loaded;
	}
}



